package cn.wolfcode.controller;

import cn.wolfcode.annotation.RequireLogin;
import cn.wolfcode.annotation.UserParam;
import cn.wolfcode.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.domain.*;
import cn.wolfcode.query.StrategyCommentQuery;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.service.*;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService themeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        //攻略阅读数+1
        strategyStatisVORedisService.viewnumIncrease(id, 1);


        return JsonResult.success(strategy);

    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));

    }

    @GetMapping("/themes")
    public Object themes(){

        return JsonResult.success(themeService.findAll());

    }
    //评论添加
    @RequireLogin
    @PostMapping("addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        //设置用户信息
        //属性拷贝
        //参数1源对象  2目标对象
        //将源对象的属性拷贝到目标对象  前提属性名必须一样   实现原理内省机制  用户id无法拷贝 属性不一样

        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);

        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(), 1);

        return JsonResult.success();
    }


    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){

        return JsonResult.success(strategyCommentService.query(qo));

    }
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid, userInfo.getId());
        return JsonResult.success();

    }

    @GetMapping("/statisVo")
    public Object statisVO(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }

    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){
        //true 表示顶成功   false 表示今天已经顶过了
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
}
